
<template>
    <div class="article-list">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh" :success-text="refreshText" success-duration="1500">
        <van-list
         v-model="loading"
         :finished="finished"
         finished-text="没有更多了"
         @load="onLoad"
         :error.sync="error"
        error-text="请求失败，点击重新加载"
        >
        <article-item v-for="(article , ind) in list" :key="ind" :article="article"></article-item>
</van-list>
</van-pull-refresh>
    </div>
  </template>

<script>
import { getArticles } from '@/api/article'
import articleItem from '@/components/articleItem'
export default {
  name: 'ArticleList',
  components: {
    articleItem
  },
  props: {
    channel: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: null,
      error: false,
      refreshText: '',
      isLoading: false
    }
  },
  computed: {},
  watch: {},
  created () {
  },
  mounted () {},
  methods: {
    async onRefresh () {
      try {
        const { data } = await getArticles({
          channel_id: this.channel.id,
          timestamp: Date.now(),
          with_top: 1
        })

        // eslint-disable-next-line camelcase
        const { results } = data.data
        this.list.unshift(...results)
        this.refreshText = `刷新成功，更新了${results.length}条数据`
        this.isLoading = false
      } catch (err) {
        window.console.log(err)
        this.isLoading = false // 关闭 loading 效果
        this.$toast('刷新失败')
      }
    },
    async  onLoad () {
      try {
        const { data } = await getArticles({
          channel_id: this.channel.id,
          timestamp: this.timestamp || Date.now(),
          with_top: 1
        })
        window.console.log(data)
        // eslint-disable-next-line camelcase
        const { pre_timestamp, results } = data.data
        this.list.push(...results)
        this.loading = false
        if (results.length) {
          // eslint-disable-next-line camelcase
          this.timestamp = pre_timestamp
        } else {
          this.finished = true
        }
      } catch (err) {
        window.console.log(err)
        this.loading = false // 关闭 loading 效果
        this.error = true // 开启错误提示
      }
    }

  }
}
</script>

  <style scoped lang="less">

.article-list{
  height: 79vh;
  overflow-y: auto;
}
</style>
